﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PocketEngine.Images;
using System.Threading;
namespace PocketEngine.Archive
{
    public class pArchiveThread 
    {
        public bool Finished
        {
            get
            {
                return mFin;
            }
        }
        public pArchiveEntry Entry
        {
            get
            {
                return mEntry;
            }
            set
            {
                mEntry = value;
            }
        }
        private pArchiveEntry mEntry = null;
        public virtual void Perform()
        {
        }
        private bool mFin = false;
    }
    public class pSaveThread : pArchiveThread
    {
        public override void Perform()
        {
            
            FileStream fs = new FileStream(Entry.EntryID + ".stream", System.IO.FileMode.Create, FileAccess.Write);
            Entry.Write(new BinaryWriter(fs));
            fs.Close();

        }
    }
    public class pLoadThread : pArchiveThread
    {
    }
    public class pArchiveImage : pArchiveEntry
    {
        public pImage Image
        {
            get
            {
                return mImg;
            }
            set
            {
                mImg = value;
            }
        }
        private pImage mImg = null;
        public override void Write(BinaryWriter w)
        {
            MemoryStream s = mImg.ToStream();
            //mImg.ToStream().Read(Buffer, 0, mImg.Width * mImg.Height, mImg.BytesPerPixel);
            int siz = mImg.Width * mImg.Height * mImg.BytesPerPixel;
            while (siz > 0)
            {
                w.Write(s.ReadByte());
                siz--;
            }
        }
     

    }
    public class pArchiveEntry
    {
        public virtual void Write(BinaryWriter w)
        {
        }
        public string EntryID
        {
            get
            {
                return mEntry;
            }
            set
            {
                mEntry = value;
            }
        }
        private string mEntry = "None";
        private int DataStart;
        private int DataLength;
        public pArchive Link = null;
        private List<pSaveThread> Saving = new List<pSaveThread>();
        private List<pLoadThread> Loading = new List<pLoadThread>();
        public pArchiveEntry(pArchive link, int ds, int dl, string name)
        {
            Link = link;
            DataStart = ds;
            DataLength = dl;
            mEntry = name;
        }
    }
    public class pArchive
    {
        public enum ArcMode
        {
            Output,Input,Both
        }
        public ArcMode FileMode
        {
            get
            {
                return mArcMode;
            }
         
        }
        private ArcMode mArcMode;
        private FileStream mFs;
        private TextWriter mTw;
        
        public void WriteImage(string id,pImage img)
        {
            FastMath.Matrix.
            pArchiveImage pImg = new pArchiveImage();
            pImg.EntryID = id;
            WriteIndex(pImg as pArchiveEntry);
        }
        public void WriteIndex(pArchiveEntry e)
        {
            mTw.WriteLine(e.EntryID);
            if(e is pArchiveImage)
            {
                mTw.WriteLine("pImage");
            }
        }
        public static pArchive WriteArc(string path)
        {
            pArchive arc = new pArchive();
            arc.mArcMode = ArcMode.Output;
            arc.mFs = new FileStream(path, System.IO.FileMode.Create, FileAccess.Write);
            arc.mTw = new StreamWriter(arc.mFs);
            return arc;

        }
         
        public void Close()
        {
            mFs.Close();
            mTw = null;
            mFs = null;
        }
        /// <summary>
        /// Use the static functions within parchive to open/create/write arc files. OR ELSE! ok no or else, but you know, it wouldn't be the worst decision you ever made :)
        /// </summary>
        public pArchive()
        {
        }
    }
}
